package controllers

import (
	"fmt"
	"math"
	"myerp/models"
	pg "myerp/src"
	"strconv"
	"time"
)

//人事管理
type PersonnelController struct {
	CommonController
}

//添加分类
func (c *PersonnelController) Add() {
	if c.Ctx.Request.Method == "POST" {
		reData := make(map[string]interface{})
		reData["status"] = 1
		var md models.Personnel
		if err := c.ParseForm(&md); err != nil {
			//handle error
		}

		//附件上传
		f, h, err := c.GetFile("file")

		fmt.Println(err)
		if err == nil {
			err = c.SaveToFile("file", "static/upload/"+h.Filename) // 保存位置在 static/upload, 没有文件夹要先创建
			fmt.Println(err)
			md.Pfile = "static/upload/" + h.Filename
			defer f.Close()
		}

		md.Ctime = int(time.Now().Unix())
		md.Uid = int(UserInfo.Id)
		_, err = models.AddPersonnel(&md)
		if err != nil {
			c.Data["satus"] = 0
			c.Data["message"] = "添加失败"
			c.Data["uri"] = "/personnel/add"
		} else {
			c.Data["message"] = "添加成功"
			c.Data["uri"] = "/personnel/list"
			c.Data["st"] = 1
		}
		c.TplName = "error.html"
		return
	}
	//获取以及分类
	l, _ := models.GetAllSortsByPid(0)
	c.Data["ActionName"] = "添加员工"
	c.Data["ol"] = l

	c.Layout = "base.html"
	c.TplName = "personnel/add.html"
}

//列表
func (c *PersonnelController) List() {
	//pid, _ := c.GetInt("pid", 0)
	//l, _ := models.GetAllClientsByPid(pid)
	var query map[string]string
	var fields []string
	var sortby []string
	var order []string
	var offset int64
	var limit int64 = 10
	var page int64
	count, err := models.GetPersonnelCount(query)
	fmt.Println("000000000000", err)
	fmt.Println(count)
	page, _ = c.GetInt64("page", 1)
	if page <= 1 {
		offset = 0
	} else {
		offset = (page - 1) * limit
	}
	if page*limit > count {
		if limit > count {
			offset = 0
		} else {
			s := math.Floor(float64(count)/float64(limit)) * float64(limit)
			offset = int64(s)
		}
	}

	l, err := models.GetAllPersonnel(query, fields, sortby, order, offset, limit)
	fmt.Println(err)
	var pages pg.Page = pg.NewPage(int(page), 10, int(count), "/personnel/list")
	c.Data["page"] = pages.Show()
	c.Data["ActionName"] = "员工列表"
	c.Data["List"] = l
	c.Layout = "base.html"
	c.TplName = "personnel/list.html"

}

//删除分类
func (c *PersonnelController) Delete() {
	Id := c.GetString("id")
	resId, _ := strconv.Atoi(Id)
	err := models.DeleteClients(resId)
	reData := make(map[string]interface{})
	reData["status"] = 0
	reData["msg"] = "删除失败！"
	if err == nil {
		reData["status"] = 1
		reData["msg"] = "删除成功"
	}
	c.Data["json"] = reData
	c.ServeJSON()
}

//js获取列表
func (c *PersonnelController) Ajaxlist() {
	reData := make(map[string]interface{})
	reData["status"] = 0
	pid, err := c.GetInt("pid")
	if err != nil || pid < 1 {
		reData["tl"] = ""
		c.Data["json"] = reData
		c.ServeJSON()
		return
	}
	l, _ := models.GetAllSortsByPid(pid)
	reData["tl"] = l
	reData["status"] = 1
	c.Data["json"] = reData
	c.ServeJSON()
}

//编辑
func (c *PersonnelController) Edite() {
	if c.Ctx.Request.Method == "POST" {
		//reData := make(map[string]interface{})
		//reData["status"] = 1
		var md models.Personnel
		if err := c.ParseForm(&md); err != nil {
			//handle error
		}

		//附件上传
		f, h, err := c.GetFile("file")
		if err == nil {
			defer f.Close()
			fmt.Println(err)
			err = c.SaveToFile("file", "static/upload/"+h.Filename) // 保存位置在 static/upload, 没有文件夹要先创建
			fmt.Println(err)
			md.Pfile = "static/upload/" + h.Filename
		}
		md.Uid = int(UserInfo.Id)
		err = models.UpdatePersonnelById(&md)
		if err != nil {
			c.Data["st"] = 0
			c.Data["message"] = "修改失败"
			c.Data["uri"] = "/personnel/edite?id=" + c.GetString("Id")
		} else {
			c.Data["message"] = "修改成功"
			c.Data["uri"] = "/personnel/list"
			c.Data["st"] = 1
		}
		c.TplName = "error.html"
		return
	}
	//获取以及分类
	l, _ := models.GetAllSortsByPid(0)
	c.Data["ActionName"] = "编辑员工"
	c.Data["ol"] = l
	id, _ := c.GetInt("id", 0)
	if id == 0 {
		c.Data["message"] = "修改的员工不存在"
		c.Data["uri"] = "/personnel/list"
		c.Data["st"] = 0
		c.TplName = "error.html"
		return
	}
	sd, err := models.GetPersonnelById(id)
	if err != nil {
		c.Data["message"] = "修改的员工不存在"
		c.Data["uri"] = "/personnel/list"
		c.Data["st"] = 0
		c.TplName = "error.html"
		return
	}
	fmt.Println(sd)
	c.Data["Sdata"] = sd
	c.Layout = "base.html"
	c.TplName = "personnel/edite.html"
}
